package engine.logic.login
{
	import engine.data.ModelLocator;
	
	import flash.external.ExternalInterface;
	
	import newx.StageInfo;
	import newx.utils.debug.IO;

	public class LoginLogic implements ILoginLogic
	{
		public function LoginLogic()
		{
			if(	null	!=	_self)
				throw new Error("LoginLogic,单例错误");
			
			init();
		}
		
		static private var _self:LoginLogic;
		
		static public function getInstance():LoginLogic
		{
			if(	null	==	_self)	
				_self	=	new LoginLogic(); 
			
			return _self;
		}
		
		private var _loginLogic:ILoginLogic;
		private var _readyCallBack:Function;
		
		static public var  ServerID:String;
		static public var LoginMode:String;
		
		static public const Mode_NormalLogin:String	=	"NormalLogin";
		static public const Mode_UniteLogin:String	=	"UniteLogin";
		
		static public var Unite_Url	: String		=	"";
		
		private function init():void
		{
			var serverIp : String = ModelLocator.getInstance().serverIni.getContent( "server_1", "ip" );
			var serverPort : String = ModelLocator.getInstance().serverIni.getContent( "server_1", "port" );
			
			//先取浏览器地址，是联合运营登陆，并且已经有角色了，则直接
			try
			{
				//	
				var url:String=	ExternalInterface.call("window.location.href.toString");
//				var url:String = "http://s1.dwx.woniu.com/?nServerId=7220001&nStartupType=4&nPartnerId=7&nTimestamp=1354779547&sAccount=gyq87765019&sAdult=1&sIdentity=04500555020F5C570106145E4D425E0F0007560050581F065A0657040D01015257525453060907545150010B5651530C03575007055100&sHmacString=52FF8A3A0360CF0A0AB0CC4D793ECB97";
//				var url:String	=	"?ip=192.168.42.107&port=2001";
//				var url:String	="nServerId=7080001&nStartupType=4&nPartnerId=7&nTimestamp=1323158310&sAccount=TEST5971&sAdult=2&sIdentity=04500252040D5D510401146D7160320D0E06521C03030A010B5B540355030350045456540D0B56520503595F5155525C06025707&sHmacString=2D70D78DCB34D6581A630449DE091685";
				if(null		==	url)//IE的问题
				{
					url	=	StageInfo.systemSpace.loaderInfo.url;
				}
				
//				url = "http://192.168.42.66/DragonWar.swf?ip=192.168.42.157&port=2003";
				
//				var url:String	=	"nServerId=xxx&nStartupType=4&nPartnerId=7&nTimestamp=1323162298&sAccount=FT2169805&sAdult=2&" +
//					"sIdentity=04500252040E57500C09147F6001570E0E0953054D2570060051730B0473057005217458707E525371097B7F0D04550E00025A0255&sHmacString=F25179049157E608833A8D4C5E24185E";
			
				var params:Object=getURLParams(url);
//				var params:Object//={}; 
				//检测联合运营登录串START
				if(params)
				{
//					nStartupType=4&
//						nPartnerId=7&4
//						sAccount=RIKI001&
//						sAdult=2&
//						sIdentity=045109550C0955520700144B5D580F0807004F0252570607000301565701015406510252515A51560200080B000B550B070050&
//							nTimestamp=1294910021&
//							sHmacString=c59af4012b337538062dbbd7b56a7d56&
//							sServerIp=58.211.140.234&
//								sServerPort=80,2002
					
//					var nServerId:String	="";//有Id则从本地取服务器,没有则从URL上取
//					var ip:String	=	"192.168.42.62";
//					var port:String	=	"2001";	
//					//公共部分
//					var nStartupType:int	=	4;
//					var nPartnerId:int		=	7;
//					var sAccount:String		=	"gametest001";
//					var sAdult:String		=	"2";
//					var sIdentity:String	=	"045109550C0955540708147E757E236C7262370051501F767B560E00710A0270022200237101202402007C0177755F0F04075403585307";
//					var nTimestamp:String	=	"1294910629";
//					var sHmacString:String	=	"6CA280B5397B8F3CF6EB08238A790E46";
					
//					ModelLocator.getInstance().HandleProperties("ip", params.sServerIp);
//					ModelLocator.getInstance().HandleProperties("port",params.sServerPort);	
//					_loginLogic=new NormalLogin();
//					_loginLogic.readyCallBack=loginReadyHandler;
//					return;
					
					var ip : String = params.ip;
					var port : String = params.port;
					if(params.debug)ModelLocator.getInstance().HandleProperties("debug",params.debug);
					if(ip	&&	port)
					{
						ModelLocator.getInstance().HandleProperties("ip", ip);
						ModelLocator.getInstance().HandleProperties("port",port);	
						_loginLogic=new NormalLogin();
						_loginLogic.readyCallBack=loginReadyHandler;
						return;
					}
					
					var nServerId:String	=	params.nServerId;//有Id则从本地取服务器,没有则从URL上取
					LoginLogic.ServerID		=	nServerId;
					ip	=	params.sServerIp;
					port	=	params.sServerPort;	
					//公共部分
					var nStartupType:int	=	int(	params.nStartupType	);
					var nPartnerId:int		=	int(	params.nPartnerId	);	
					var sAccount:String		=	params.sAccount;
					var sAdult:String		=	params.sAdult;
					var sIdentity:String	=	params.sIdentity;
					var nTimestamp:String	=	params.nTimestamp;
					var sHmacString:String	=	params.sHmacString;
					
					
					if(params.sServerIp && params.sServerPort)
					{
						ModelLocator.getInstance().HandleProperties("ip",params.sServerIp);
						ModelLocator.getInstance().HandleProperties("port",params.sServerPort);
					}
					else
					{
						ModelLocator.getInstance().HandleProperties("ip",params.ip);
						ModelLocator.getInstance().HandleProperties("port",params.port);
						
						ip	=	params.ip;
						port	=	params.port;	
					}
					ModelLocator.getInstance().HandleProperties("serverId","-1");
					
					var loginInfo:Object=new Object();
					if(nServerId)
					{
						var serverKey:String;
						for(var i:uint=1;	i<=10;	i++)
						{
							if(ModelLocator.getInstance().serverIni.getContent("server_"+i,"serverid")	==	nServerId)
							{
								serverKey	=	"server_"+i;
								break;
							}
						}
						
						ip=ModelLocator.getInstance().serverIni.getContent(serverKey,"ip");
						port=ModelLocator.getInstance().serverIni.getContent(serverKey,"port");
						
						ModelLocator.getInstance().HandleProperties("serverId",nServerId);
//						ModelLocator.getInstance().HandleProperties("sAccount",n);
						
						IO.traceLog("本地IP："+ip+"-----端口："+port);
					}
					else
					{
						ModelLocator.getInstance().HandleProperties("serverId",-1);
					}
					
					ModelLocator.getInstance().player.sAccount=	sAccount;
					
					//===串拼接====
					if(4 ==	nStartupType && nPartnerId == 7 )
					{
//						@{nPartnerId},{sAdult},{sIdentity},{nTimestamp},{sHmacString}
						sIdentity	=	"@"+nPartnerId+","
												+sAdult+","
												+sIdentity+","
												+nTimestamp+","
												+sHmacString;
					}
					else
					{
//						{nPartnerId},{sAdult},{sIdentity},{nTimestamp},{sHmacString} 
						sIdentity	=	String(nPartnerId)+","
										+sAdult+","
										+sIdentity+","
										+nTimestamp+","
										+sHmacString;
					}
					
//					sIdentity = "@7,1,04500252070A52550208147F6001570E0E0953054D2570060051730B0473057005217458707E525371097B7F0D04550E00025A0255,1323227779,6252D61FF124266C8A66AE44C2A2EFBA";
//					sIdentity ="@7,1,04500252070B51570501147F6001570E0E0953054D2570060051730B0473057005217458707E525371097B7F0D04550E00025A0255,1323234500,ECDC75131DF4870EF06EB7DB72EAFFBA";
					//=======
					
					//							loginInfo.sAccount,loginInfo.sIdentity
					if(ip	&&	port	&& sAccount	&&	sIdentity)
					{
						Unite_Url		=	url;
						
						loginInfo.ip=ip;
						loginInfo.port=port
						loginInfo.sAccount=sAccount;
						loginInfo.sIdentity=sIdentity;
						LoginGroup._sIdentity = sIdentity;
						_loginLogic=new UniteLogin(loginInfo);
					}
				}
			}
			catch(e:*)
			{
//				ModelLocator.getInstance().HandleProperties("ip", "192.168.42.60");
				
				ModelLocator.getInstance().HandleProperties("ip", serverIp);
				ModelLocator.getInstance().HandleProperties("port",serverPort);
				
				_loginLogic=new NormalLogin();
			}
			
			
			if(	null	==	_loginLogic	)
			{
//				ModelLocator.getInstance().HandleProperties("ip", "192.168.42.60");
//				if( !ModelLocator.getInstance().CheckProperties("ip")  )
//				{
				ModelLocator.getInstance().HandleProperties("ip", serverIp);
					ModelLocator.getInstance().HandleProperties("port",serverPort);
//				}
				_loginLogic	=	new NormalLogin();
			}
			
			_loginLogic.readyCallBack=loginReadyHandler;
			
			if(_loginLogic is UniteLogin)
			{
				LoginCallLater.setReady(LoginCallLater.ConfigDataReady);
				LoginCallLater.setReady(LoginCallLater.UiResReady);
			}
		}
		
		private function loginReadyHandler():void
		{
			if(	null	!=	_readyCallBack	)
								_readyCallBack();
		}
		
		
		public function set readyCallBack(cb:Function):void
		{
			_readyCallBack=cb;	
			
			if(	(_loginLogic is UniteLogin)	&&	(	null	!=	_readyCallBack)	)
													_readyCallBack();
		}
		
		private function getURLParams(url:String):Object
		{
			var params:Object=new Object();
			var startPos:int=url.indexOf("?");
			
			var paramStr:String=url.substr(startPos+1);
			
			var paramArr:Array=paramStr.split("&");
			var i:uint;
			var len:uint=paramArr.length;
			var item:Array;
			for(i=0;	i<len;	i++)
			{
				item=String(paramArr[i]).split("=");
				
				params[	item[0]	]=	item[1];	
				IO.traceLog("--------参数名：",item[0],"---------参数值：",item[1]);
			}
			return params;
		}
	}
}